<%@ page import="java.sql.Statement" import="java.sql.ResultSet" import="java.sql.Connection" %> 
<%@page import="connection.DBConnection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
function sub()
{
	var t=0.0;
	
		var a=document.getElementById("innamt").value;
	var b=document.getElementById("amtadjust").value;
	
	var c=parseFloat(a)-parseFloat(b);
	document.getElementById("balamt").value=c;
	
	t=parseFloat(t)+parseFloat(b);
	
	if(b>a)
		{
		var d=b-a;
		document.getElementById("advance").value=d;
		
		
		}	
	document.getElementById("total").value=t;
	}
</script>
</head>
<body>
<h2 align="center"> Supplier Invoice wise outstanding</h2>
<form name="bankledgerreport" method="get" action=SupplierInvoiceServlet>

<table align="center" border=5 width=700>
<tr><th>Date</th>
<th>Invoice Number</th>
<th>Invoice Amount</th>
<th>Amount Adjusted</th>
<th>Balance Amount</th></tr>
<%  DBConnection dbc=new DBConnection();
			Connection con=dbc.getNewConnection();
			Statement st = null;
			ResultSet rs = null;
			String selectedpycode = (String) request.getAttribute("pycode");
			int count=1;
			
			double bal=0.0,in=0.0,adj=0.0;
			
			String dte="",nm="",cd="",inn="";
			%>
			<%! int cnt=0; %>
			<%
			try
			{
				st=con.createStatement() ;
				 rs=st.executeQuery("select count(*) from purchaseinvoiceoutstanding where scode= '"+selectedpycode+"'");
				  while(rs.next())
				  {
					  cnt=rs.getInt(1);
				  }
				  
				  
					  if(cnt>0)
					  {
						  
						  
						  st=con.createStatement() ;
						  rs=st.executeQuery("select date,pvname,scode,pvinno,balance,adjamount,pvcred from purchaseinvoiceoutstanding  where scode='"+selectedpycode+"' group by pvinno;");
							while(rs.next()){
								
								dte=rs.getString(1);
								nm=rs.getString(2);
								cd=rs.getString(3);
								inn=rs.getString(4);
								 in=rs.getDouble(5);
								 adj=rs.getDouble(6); 
								 int cred=rs.getInt(7);
								 %>
								 
								 
								 <input type=hidden name=sname value="<%=nm%>"/>
			  <input type=hidden name=scd value="<%=cd%>"/>
			  <input type=hidden name=cred value="<%=cred%>"/>
			 <tr><td><input type=hidden  id=dte name=dte value="<%=dte %>"/><%=dte %></td>
			 
			 <td><input type=hidden id=inn name=inn value="<%=inn %>"/><%=inn %></td>
			<td><input type=hidden id=inn name=innamt value="<%=in %>"/><%=in %></td>
		
			 <td><input type=text name=amtadjust id=amtadjust value="0" onblur="sub()" /></td>
			 <td><input type=text name=balamt id=balamt value="<%=in%>"/></td>
			
			</tr>
								 
								 <%
					  }}
					  else{
				  
				  rs=st.executeQuery("select date,pvsname,scode,pvinno,pvtota,pvcredlim from purchasevoucher2 where scode='"+selectedpycode+"' group by pvinno;");
				  
			
			while(rs.next()){
			
			dte=rs.getString(1);
			nm=rs.getString(2);
			cd=rs.getString(3);
			inn=rs.getString(4);
			 in=rs.getDouble(5);
			 int cred1=rs.getInt(6);
			 
			%>
			<input type=hidden name=sname value="<%=nm%>"/>
			  <input type=hidden name=scd value="<%=cd%>"/>
			 <input type=hidden name=cred value="<%=cred1%>"/>
			 <tr><td><input type=hidden  id=dte name=dte value="<%=dte %>"/><%=dte %></td>
			 
			 <td><input type=hidden id=inn name=inn value="<%=inn %>"/><%=inn %></td>
			<td><input type=hidden id=inn name=innamt value="<%=in %>"/><%=in %></td>
		
			 <td><input type=text name=amtadjust id=amtadjust value="0" onblur="sub()" /></td>
			 <td><input type=text name=balamt id=balamt value="<%=in %>"/></td>
			
			</tr>
			 <% }}
			   }
			   catch(Exception e)
			   {} %>
			  
			   <tr><td></td><td>Advanced Received</td><td></td><td><input type=text name=advance id=advance value="0"/></td><td></td></tr>
			   <tr><td></td><td>Total</td><td></td><td><input type=text name=total id=total value="0"/></td><td></td></tr>
 </table>
 <br><br>
<center> <input type=submit value=Save></center>
		</form>	   
			   
</body>
</html>